﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using XNACamera;

namespace PlaneGame
{
    class GameModel : DrawableGameComponent
    {
        Model _model; //modele 3D à rendre
        String _nomFichier; //Le nom de l'asset contenant le modèle
        Camera _camera; //la caméra à utiliser pour le rendu
        Matrix _world; //la matrice de placement

        public Model Model
        {
            get
            {
                return _model;
            }

            set
            {
                _model = value;
            }
        }

        public String NomFichier
        {
            get
            {
                return _nomFichier;
            }

            set
            {
                _nomFichier = value;
            }
        }

        public Camera Camera
        {
            get
            {
                return _camera;
            }

            set
            {
                _camera = value;
            }
        }

        virtual public Matrix World
        {
            get
            {
                return _world;
            }

            set
            {
                _world = value;
            }
        }

        public GameModel(Game game)
            :base(game)
        {
            _world = Matrix.Identity;
        }

        public GameModel(Game game, String assetName)
            : base(game)
        {
            _world = Matrix.Identity;
            _nomFichier = assetName;
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _model = Game.Content.Load<Model>(_nomFichier);
            
        }

        public override void Draw(GameTime gameTime)
        {
            // TODO: Add your drawing code here
            DrawModel(_model, _camera);

            base.Draw(gameTime);
        }

        private void DrawModel(Model m, Camera c)
        {
            Matrix[] transforms = new Matrix[m.Bones.Count];
            m.CopyAbsoluteBoneTransformsTo(transforms);


            foreach (ModelMesh mesh in m.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.View = c.View;
                    effect.Projection = c.Projection;
                    effect.World = transforms[mesh.ParentBone.Index] * World;


                }

                mesh.Draw();
            }
        }

        public override void Update(GameTime gameTime)
        {
            
        }
    }
}